package com.taotao.web.service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.taotao.common.ApiService.ApiService;
import com.taotao.common.ApiService.HttpClientResult;

@Service
public class IndexPageService {
	
	@Autowired
	private PropertieService propertiesService;
	
	@Autowired
	private ApiService apiService;
	
	public String queryIndexAd1(){
		try {
			String url = propertiesService.TT_MANAGE_URL+ propertiesService.TT_MANAGE_AD1_URL;
			// 查询后台接口，返回结果对象HttpClientResult
			HttpClientResult result = apiService.doGet(url);
			// 获取返回的json数据
			String content = result.getContent();
			// 解析json数据
			if(StringUtils.isNotBlank(content)){
				// 使用 readTree方法解析数据
				JsonNode jsonNode = PropertieService.MAPPER.readTree(content);
				// 根据key获取value
				//int total = jsonNode.get("total").asInt();
				ArrayNode arrNode = (ArrayNode) jsonNode.get("rows");
				
				// 创建集合封装数据: List<Map<String, String>>
				ArrayList<Map<String, Object>> dataList = new ArrayList<Map<String, Object>>();
				
				for (JsonNode json : arrNode) {
					Map<String, Object> map = new HashMap<String, Object>();
					map.put("srcB", json.get("pic2").asText());
					map.put("height", 240);
					map.put("alt", "");
					map.put("width", 670);
					map.put("src", json.get("pic").asText());
					map.put("widthB", 550);
					map.put("href", json.get("url").asText());
					map.put("heightB", 240);
					dataList.add(map);
				}
				// 返回数据
				return PropertieService.MAPPER.writeValueAsString(dataList);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	public String queryIndexAd2() {
		try {
			String url = propertiesService.TT_MANAGE_URL+ propertiesService.TT_MANAGE_AD2_URL;
			// 查询后台接口，返回结果对象HttpClientResult
			HttpClientResult result = apiService.doGet(url);
			// 获取返回的json数据
			String content = result.getContent();
			// 解析json数据
			if(StringUtils.isNotBlank(content)){
				// 使用 readTree方法解析数据
				JsonNode jsonNode = PropertieService.MAPPER.readTree(content);
				// 根据key获取value
				//int total = jsonNode.get("total").asInt();
				ArrayNode arrNode = (ArrayNode) jsonNode.get("rows");
				
				// 创建集合封装数据: List<Map<String, String>>
				ArrayList<Map<String, Object>> dataList = new ArrayList<Map<String, Object>>();
				
				for (JsonNode json : arrNode) {
					Map<String, Object> map = new HashMap<String, Object>();
					map.put("srcB", json.get("pic2").asText());
					map.put("height", 70);
					map.put("alt", "");
					map.put("width", 310);
					map.put("src", json.get("pic").asText());
					map.put("widthB", 210);
					map.put("href", json.get("url").asText());
					map.put("heightB", 70);
					dataList.add(map);
				}
				// 返回数据
				return PropertieService.MAPPER.writeValueAsString(dataList);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
}








